package com.ruoyi.douyin.service.imp;

import com.ruoyi.common.constant.ConstantDouYinBase;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.vo.AuthVo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.AjaxUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.douyin.service.FansService;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FansServiceImp implements FansService {
    @Autowired
    private AjaxUtils ajaxUtils;
    @Autowired
    private RedisCache redisCache;

    /**
     * 粉丝列表
     */
    @Override
    public AjaxResult fansList(List<String> originOpenId) {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        List<String> originOpenId = loginUser.getOpenId();
        ArrayList<Object> objects = new ArrayList<>();
        for (String openId : originOpenId) {
            if (openId == null) return AjaxResult.error("未授权");
            AuthVo authVo = redisCache.getCacheObject(openId);
            if (authVo == null) {
                return AjaxResult.error("授权过期");
            }
            String url = ConstantDouYinBase.Url.BASE_URL.getUrl() + ConstantDouYinBase.Url.USER_FANS_LIST.getUrl();
            Map<String, Object> param = new HashMap<>();
            param.put("open_id", authVo.getOpenId());
            param.put("access_token", authVo.getAccessToken());
            param.put("count", 10);
            try {
                objects.add(ajaxUtils.doGetWithParams(url, param).get("data"));
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error();
            }
        }
        return AjaxResult.success("success", objects);
    }
}
